<?php
// +----------------------------------------------------------------------
// | Created by PHPstorm: [ JRK丶Admin ]
// +----------------------------------------------------------------------
// | Copyright (c) 2019~2022 [LuckyHHY] All rights reserved.
// +----------------------------------------------------------------------
// | SiteUrl: http://www.luckyhhy.cn
// +----------------------------------------------------------------------
// | Author: Lucky
// +----------------------------------------------------------------------
// | Date: 2020-08-14 16:24:03
// +----------------------------------------------------------------------
// | Description:
// +----------------------------------------------------------------------

namespace app\admin\model;

use app\admin\validate\CheckStudent;
use app\common\impl\Comm;
use app\common\model\AdminBaseModel;
use think\Exception;
use think\facade\Db;
use Jrk\Tree;
use think\model\concern\SoftDelete;
use think\facade\Route;
use app\admin\validate\CheckArticle;
use Jrk\Tool;

class Student extends AdminBaseModel
{

//    use SoftDelete;
    protected $defaultSoftDelete = 0; //默认值

    //表名
    protected $name = "student";

    public function getAdminPageData($param = [], $order = 'id asc')
    {
        $where = [];
        //名称查询
        if (isset($param['name']) && $param['name'] != '') {
            $where[] = ['name', '=', $param['name']];
        }
        try {
            $data = self::where($where)->order($order)->select()->toArray();
            $count = self::where($where)->count("id");

            return parent::ajaxResult($data, $count);
        } catch (Exception $exception) {
            return parent::ajaxResult([], 0, 100, $exception->getMessage());
        }
    }

    /**
     * @param $param
     * @return \think\response\Json
     * @author:
     * @describe:编辑添加
     */
    public function doAll($param)
    {
        // TODO: Implement doAll() method.
        try {
            $param['password'] = md5($param['password']);
            $check = new CheckStudent();
            if (!$check->check($param)) {
                return parent::JsonReturn($check->getError(), 0);
            }

            if (isset($param['id'])) {
                $res = self::update($param);
            }else{
                $res = self::save($param);
            }

            if ($res) {
                return self::JsonReturn("更新添加成功");
            } else {
                return self::JsonReturn("更新添加失败", 0);
            }
        } catch (\Exception $e) {
            return self::JsonReturn($e->getMessage(), 0);
        }
    }

    /**
     * @param $id
     * @return \think\response\Json
     * @author: 
     * @describe:软删除
     */
    public function delById($id)
    {
        try {
            // TODO: Implement delById() method.
            if (is_array($id)) {
                $ids = $id;
            } else {
                $ids = @explode(",", $id);
            }
            $result = self::destroy($ids);
            if ($result) {
                return self::JsonReturn("删除成功");
            } else {
                return self::JsonReturn("删除失败", 0);
            }
        } catch (\Exception $exception) {
            return self::JsonReturn($exception->getMessage(), 0);
        }
    }
}